/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int sizea = 0;
    int sizeb = 0;
    int gap = 0;
    if (headA == NULL || headB == NULL)
        return NULL;
    ListNode* pcura = headA;
    ListNode* pcurb = headB;
    while (pcura)
    {
        sizea++;
        pcura = pcura->next;
    }
    while (pcurb)
    {
        sizeb++;
        pcurb = pcurb->next;
    }
    gap = (sizea > sizeb) ? (sizea - sizeb) : (sizeb - sizea);
    ListNode* shortlist = headA;
    ListNode* longlist = headB;
    if (sizea > sizeb)
    {
        shortlist = headB;
        longlist = headA;
    }
    while (gap--)
    {
        if (longlist->next != NULL)
            longlist = longlist->next;
    }
    while (shortlist)
    {
        if (shortlist == longlist)
            return shortlist;
        shortlist = shortlist->next;
        longlist = longlist->next;
    }
    return NULL;
}